package hbase;


import org.apache.hadoop.hbase.util.Bytes;

import java.math.BigInteger;
import java.security.MessageDigest;

/**
 * @author:huanqing.cheng
 * @time:2022/2/23 6:17 下午
 * @description:
 */
public class Md5SaltedTest {
    public static void main(String[] args) throws Exception {

        String rowkey = "hello_hbase";
        String saltedRowKey = saltRowKey(rowkey, MessageDigest.getInstance("MD5"));
        System.out.println("Salted rowkey is " + saltedRowKey);

    }

    private static String saltRowKey(String rowkey, MessageDigest message) {
        byte[] digest = message.digest(Bytes.toBytes(rowkey));
        // To lower case is a must here. Currently not support upper case.
        String md5 = new BigInteger(1, digest).toString(16).toLowerCase();
        System.out.println("MD5 for row key " + rowkey + " is " + md5);

        StringBuilder builder = new StringBuilder();
        builder.append(md5, 0, 2) // Also we can get the last 2: [length-2, length), it's up to you
                .append(":")       // Add a delimiter or not it's up to you
                .append(rowkey);   // Finally, we append the row key
        return builder.toString();
    }


}
